package com.tqz.rc.server.code;

import com.tqz.rc.server.listener.StartingApplicationListener;
import com.tqz.rc.server.listener.TqzRcApplicationListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/9/24 11:34
 */
public class TqzRcSpringApplicationRunListener implements SpringApplicationRunListener, Ordered {

    private final SpringApplication application;

    private final String[] args;

    private List<TqzRcApplicationListener> tqzRcApplicationListeners = new ArrayList<>();

    {
        tqzRcApplicationListeners.add(new StartingApplicationListener());
    }

    public TqzRcSpringApplicationRunListener(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        for (TqzRcApplicationListener listener : tqzRcApplicationListeners) {
            listener.environmentPrepared(environment);
        }
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}
